Exponential Backoff এবং Delayed Retry Techniques গাইড ও নোট

Java Technologies - গুয়াভা (Guava) - Guava Retryer
300

Exponential Backoff:

Exponential Backoff হল একটি সাধারণ অ্যালগোরিদমিক কৌশল যেখানে সিস্টেম ত্রুটি (error) বা ব্যর্থ অনুরোধ (failed request) পুনরায় চালানোর সময় বিলম্ব (delay) ধীরে ধীরে বৃদ্ধি করে।

কেন প্রয়োজন?

  • সিস্টেম ওভারলোড বা API লিমিটেশন এড়ানো।
  • প্রতিক্রিয়ার সম্ভাবনা বাড়ানোর জন্য পুনরায় চেষ্টা (retry) বিরতি সামঞ্জস্য করা।

কাজ করার পদ্ধতি:

  1. প্রথম বিলম্ব: একটি নির্দিষ্ট ক্ষুদ্র ডিলে (e.g., 100ms) দিয়ে শুরু হয়।
  2. প্রতি ব্যর্থতার পর বিলম্ব বৃদ্ধি: প্রতিটি ব্যর্থতায় দ্বিগুণ (2x) সময় যোগ করা হয়।
    • উদাহরণ: 100ms → 200ms → 400ms → 800ms ...
  3. সর্বাধিক বিলম্ব সীমা: একটি নির্ধারিত সর্বাধিক সীমায় (e.g., 30 seconds) থেমে যায়।

Guava-এ Exponential Backoff বাস্তবায়ন:

Guava লাইব্রেরি সরাসরি Exponential Backoff প্রদান না করলেও এটি কার্যকরভাবে বাস্তবায়নে সাহায্য করতে পারে। Retrying এবং RateLimiter ব্যবহার করে এটি সামলানো যায়।

উদাহরণ:
import java.util.concurrent.TimeUnit;

public class ExponentialBackoffExample {
    public static void main(String[] args) {
        int maxRetries = 5;
        long delay = 100; // Initial delay in milliseconds
        
        for (int attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                // Try to execute the task
                performTask();
                break; // If successful, exit loop
            } catch (Exception e) {
                System.out.println("Attempt " + attempt + " failed. Retrying...");
                if (attempt < maxRetries) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(delay);
                        delay *= 2; // Double the delay for next retry
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        throw new RuntimeException("Retry interrupted", ie);
                    }
                } else {
                    System.out.println("Max retries reached. Giving up.");
                }
            }
        }
    }

    private static void performTask() throws Exception {
        // Simulate task logic (can throw exceptions)
        throw new Exception("Simulated task failure");
    }
}

Delayed Retry:

Delayed Retry হল একটি প্রক্রিয়া যেখানে সিস্টেম নির্ধারিত সময় পরে পুনরায় কাজ শুরু করে। এটি Exponential Backoff এর সাথে যুক্ত হতে পারে অথবা নির্দিষ্ট সময় ব্যবধানে কাজ করতে পারে।

Guava-এর TimeLimiter ব্যবহার করে বিলম্বিত পুনরায় চেষ্টা:

Guava-এর TimeLimiter এবং Java-এর ScheduledExecutorService একত্রে ব্যবহার করে বিলম্বিত পুনরায় চেষ্টা করা যায়।

উদাহরণ:
import java.util.concurrent.*;

public class DelayedRetryExample {
    public static void main(String[] args) {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        int maxRetries = 3;
        long delay = 2000; // Delay in milliseconds
        
        Runnable task = new Runnable() {
            int attempt = 0;

            @Override
            public void run() {
                attempt++;
                try {
                    performTask();
                    System.out.println("Task succeeded on attempt " + attempt);
                    scheduler.shutdown(); // Stop retrying
                } catch (Exception e) {
                    System.out.println("Attempt " + attempt + " failed.");
                    if (attempt >= maxRetries) {
                        System.out.println("Max retries reached. Task failed.");
                        scheduler.shutdown();
                    }
                }
            }
        };

        // Schedule the task with delay
        scheduler.scheduleWithFixedDelay(task, 0, delay, TimeUnit.MILLISECONDS);
    }

    private static void performTask() throws Exception {
        // Simulate task logic
        throw new Exception("Simulated task failure");
    }
}

  • Exponential Backoff এবং Delayed Retry, উভয় কৌশলই Java এবং Guava-এ সহজে বাস্তবায়নযোগ্য।
  • Exponential Backoff API কলের ক্ষেত্রে কার্যকর যেখানে দ্রুত ত্রুটি সাড়া প্রয়োজন।
  • Delayed Retry বিলম্বিত এবং নির্ধারিত পুনরায় চেষ্টা ব্যবস্থা প্রদান করে।

প্রয়োগ নির্ভর করবে অ্যাপ্লিকেশনের প্রয়োজনীয়তা এবং ব্যর্থতার ধরন অনুযায়ী।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...